/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package quanlysieuthi;
import DAO.*;
import DTO.*;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author quangphuc
 */
public class FrmDonHang extends javax.swing.JFrame {

    /**
     * Creates new form FrmDonHang
     */
    public FrmDonHang() {
        initComponents();
        this.LoadComBoBox();
    }

    List<CChiTietHoaDon> listChiTiet = new ArrayList<CChiTietHoaDon>();
    CHoaDon hoadon = new CHoaDon();
    public Boolean add = false;
    public Boolean edit = false;
    public Boolean delete = false;
    CHoaDonDAO hoadonDao = new CHoaDonDAO();
    CChiTietHoaDonDAO chitietDAO =new CChiTietHoaDonDAO();
    CTonKhoDAO tonkhoDAO = new CTonKhoDAO();
    List<CTon_Kho> listTonKho = new ArrayList<CTon_Kho>();
    CSanPham sanpham = new CSanPham();
    CSanPhamDAO sanphamDAO = new CSanPhamDAO();
    List<CSanPham> listSanPham = new ArrayList<CSanPham>();
    SubConfig config = SubConfig.getInstance();  
    CKho khoCurrent = new CKho();
    CKhachHang khachHang = new CKhachHang();
    List<CKhachHang> khach_hangList = new ArrayList<CKhachHang>();
    CKhacHangDAO khachangDAO = new CKhacHangDAO();
    private void LoadComBoBox(){
        config.init();
        listSanPham = sanphamDAO.getAll(config);
        listTonKho = tonkhoDAO.getAll(config);  
        khachangDAO.sWhere = " where loai = 2";
        khach_hangList = khachangDAO.getAll(config);
        config.close();
        this.cbbSanPham.removeAllItems();
        for(CSanPham sp : listSanPham){
            this.cbbSanPham.addItem(sp.getTenSanPham());
        }
        
        this.cbbKhacHang.removeAllItems();
        for(CKhachHang kh : khach_hangList){
            this.cbbKhacHang.addItem(kh.getHoTen());
        }
    }
    
    private void LoadDataTable(List<CChiTietHoaDon> list){
        Vector<Vector> result = new Vector<Vector>();
        int stt = 1;
        for(CChiTietHoaDon item :list){
            Vector<String>row = new Vector<String>();
            row.addElement("" + stt);
            row.addElement("" + item.getTenSanPham());
            row.addElement(""+item.getSoLuong());
            row.addElement(""+ item.getDongia());
            row.addElement(""+item.getThanhTien());
            result.addElement(row);
            stt++;
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("STT");
        columnNames.addElement("Ten hang");
        columnNames.addElement("Soluong");
        columnNames.addElement("Don Gia");
        columnNames.addElement("Thanh Tien");
        DefaultTableModel model = new DefaultTableModel(result, columnNames);     
        
        this.tbDanhSach.removeAll();
        this.tbDanhSach.setModel(model);        
        this.tbDanhSach.setFillsViewportHeight(true);
        this.tbDanhSach.setEnabled(false);
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblNgayNhap = new javax.swing.JLabel();
        txtNgayNhap = new javax.swing.JTextField();
        lblKhacHang = new javax.swing.JLabel();
        cbbKhacHang = new javax.swing.JComboBox();
        lblNhanVienNhap = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSach = new javax.swing.JTable();
        txtNVNhap = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cbbTinhTrangPhieu = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbbSanPham = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        txtSoLuong = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtDonGia = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnLuu = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();
        jTextField3 = new javax.swing.JTextField();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        txtThanhTien = new javax.swing.JTextField();
        btnAddChiTiet = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblNgayNhap.setText("Ngay nhap");

        lblKhacHang.setText("khach hang");

        cbbKhacHang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        lblNhanVienNhap.setText("Nhan vien nhap");

        tbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tbDanhSach);

        jLabel1.setText("Tinh trang phieu");

        cbbTinhTrangPhieu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel2.setText("Danh sach cacmat hang");

        jLabel3.setText("Nhap chi tiet cac mat hang");

        jLabel4.setText("Ten Hang");

        cbbSanPham.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("So luong nhap");

        jLabel6.setText("Don gia");

        txtDonGia.setText("txtDonGia");

        jButton1.setText("Them moi");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        btnSua.setText("Sua");

        btnLuu.setText("Luu");

        btnDelete.setText("Xoa");

        btnClose.setText("Thoat");

        jButton6.setText("<");

        jButton7.setText("<<");

        jTextField3.setText("jTextField3");

        jButton8.setText(">>");

        jButton9.setText(">");

        jLabel7.setText("thanh tien");

        btnAddChiTiet.setText("Them chi tiet");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(lblNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(txtNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel4)
                                            .addGap(18, 18, 18)
                                            .addComponent(cbbSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel1)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(cbbTinhTrangPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jButton6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(31, 31, 31)
                                        .addComponent(lblKhacHang)
                                        .addGap(40, 40, 40)
                                        .addComponent(cbbKhacHang, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(26, 26, 26)
                                        .addComponent(lblNhanVienNhap)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtNVNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 207, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton8)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton9))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(65, 65, 65)
                                        .addComponent(jLabel5)
                                        .addGap(26, 26, 26)
                                        .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(33, 33, 33)
                                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel7)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(18, 18, 18)
                                .addComponent(btnSua)
                                .addGap(18, 18, 18)
                                .addComponent(btnLuu)
                                .addGap(18, 18, 18)
                                .addComponent(btnDelete)
                                .addGap(18, 18, 18)
                                .addComponent(btnClose))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(63, 63, 63)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 865, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(btnAddChiTiet, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(370, 370, 370))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton7)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton8)
                    .addComponent(jButton9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtNgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblKhacHang)
                        .addComponent(cbbKhacHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblNhanVienNhap)
                        .addComponent(txtNVNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbbTinhTrangPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cbbSanPham, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(txtDonGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(txtThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnAddChiTiet)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(btnSua)
                    .addComponent(btnLuu)
                    .addComponent(btnDelete)
                    .addComponent(btnClose))
                .addGap(38, 38, 38))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        listChiTiet = new ArrayList<CChiTietHoaDon>();
        hoadon = new CHoaDon();        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void Save(){
        if(this.add == false && this.edit == false){
            return;
        }
        long khachHangId = 1;
        String loai_phieu ="PN";
        hoadon.setKhachHangId(khachHangId);
        hoadon.setLoaiPhieu(loai_phieu);
        long nhan_vien_lap_id =  CKiemTra.user_id;
        long nhan_vien_giao_id =1;
        hoadon.setNguoiGiaoId(nhan_vien_giao_id);
        hoadon.setNguoiLapId(nhan_vien_giao_id);
        hoadon.setNguoiSua(CKiemTra.user_name);
        hoadon.setNguoiTao(CKiemTra.user_name);
        long hoa_don_id = 1;
        config.init();
        if(this.add){
            hoa_don_id =(long)hoadonDao.insertOnSubmit(hoadon, config);
            for(CChiTietHoaDon chitiethd : this.listChiTiet){
                chitiethd.setHoaDonId(hoa_don_id);
                Object result = chitietDAO.insertOnSubmit(chitiethd, config);
                chitiethd.setId((long)result);
            }
        }
        else if(this.edit){
            
        }
        this.add = false;
        this.edit = false;
        config.close();
    }
    
    private void addChiTiet(){
        CChiTietHoaDon chitiet = new CChiTietHoaDon();
        long sanphamId = 1;
        CTon_Kho tkho = new CTon_Kho();
        tkho.setKhoId(khoCurrent.getId());
        tkho.setSanPhamId(sanphamId);
        CTon_Kho currentFind = this.tonkhoDAO.Find(tkho, listTonKho);
        int so_luong = Integer.parseInt(this.txtSoLuong.getText().trim());
        String ten_san_pham = "";
        Double don_gia = 0.0;
        Double thanh_tien = 0.0;
        if(CKiemTra.isKiemTraSoLuongNhap(tkho, so_luong)){
            chitiet.setSoLuong(so_luong);
            chitiet.setDongia(don_gia);
            chitiet.setHoaDonId(1);
            chitiet.setTenSanPham(ten_san_pham);
            thanh_tien = so_luong * don_gia;
            chitiet.setThanhTien(thanh_tien);
            this.listChiTiet.add(chitiet);
        }
        else{
            JOptionPane.showConfirmDialog(this, "Trong kho da co du hang khong nhap san pham nay nua.");
            return;
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmDonHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmDonHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmDonHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmDonHang.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmDonHang().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddChiTiet;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnSua;
    private javax.swing.JComboBox cbbKhacHang;
    private javax.swing.JComboBox cbbSanPham;
    private javax.swing.JComboBox cbbTinhTrangPhieu;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JLabel lblKhacHang;
    private javax.swing.JLabel lblNgayNhap;
    private javax.swing.JLabel lblNhanVienNhap;
    private javax.swing.JTable tbDanhSach;
    private javax.swing.JTextField txtDonGia;
    private javax.swing.JTextField txtNVNhap;
    private javax.swing.JTextField txtNgayNhap;
    private javax.swing.JTextField txtSoLuong;
    private javax.swing.JTextField txtThanhTien;
    // End of variables declaration//GEN-END:variables
}
